package cn.christ.com.mobilesafe;

import android.app.Application;
import android.content.Context;
import android.os.Handler;
import android.support.multidex.MultiDex;
import android.text.TextUtils;

import com.mcxiaoke.packer.helper.PackerNg;
import com.squareup.leakcanary.LeakCanary;
import com.tencent.bugly.crashreport.CrashReport;
import com.umeng.commonsdk.UMConfigure;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import cn.christ.com.mobilesafe.common.Const;
import cn.christ.com.mobilesafe.common.Global;
import cn.christ.com.mobilesafe.common.InitializeService;
import cn.christ.com.mobilesafe.common.utils.PackageUtil;

/**
 * Created by Christ on 2018/7/13.
 */

public class MyApp extends Application {
    public static Context context;
    private String market;
    public static Handler handler;

    @Override
    public void onCreate() {
        super.onCreate();
        this.context = this;
        this.handler = new Handler();
        InitializeService.start(this); //开启服务加子线程初始化数据，适用于没有ui相关的操作，用完即走
        Global.init(context);//初始化全局变量
        market = PackerNg.getMarket(context);
        initBugly(market); //初始化Bugly
        initUmeng(market); //初始化友盟
        initLeakCanary(); //初始化LeakCanary
    }

    @Override
    public void onTerminate() {
        // 程序终止的时候执行
        super.onTerminate();
        if (context!=null){
            context=null;
        }

        if (handler!=null){
            handler=null;
        }
    }



    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        // 将MultiDex注入到项目中
        MultiDex.install(this);
    }


    /**
     * 初始化LeakCanary
     */
    private void initLeakCanary() {
        //初始化LeakCanary
        if (LeakCanary.isInAnalyzerProcess(context)) {
            return;
        }
        LeakCanary.install(this);
    }


    /**
     * 初始化Bugly
     *
     * @param market
     */
    private void initUmeng(String market) {
        // 动态指定appkey和渠道
        UMConfigure.init(context, "5b4ab014f43e485486000066", market, 0, null);
    }

    /**
     * 初始化Bugly
     *
     * @param market
     */
    private void initBugly(String market) {
        Context context = getApplicationContext();
        // 获取当前包名
        String packageName = context.getPackageName();
        // 获取当前进程名
        String processName = getProcessName(android.os.Process.myPid());
        // 设置是否为上报进程
        CrashReport.UserStrategy strategy = new CrashReport.UserStrategy(context);
        strategy.setUploadProcess(processName == null || processName.equals(packageName));

        //设置渠道标记
        strategy.setAppChannel(market);  //设置渠道
        strategy.setAppVersion(PackageUtil.getVersionName());      //App的版本
        strategy.setAppPackageName(getPackageName());  //App的包名

        // 初始化Bugly
        CrashReport.initCrashReport(context, "fbfc8c0bd4", Const.isDebug, strategy);
        //如果通过“AndroidManifest.xml”来配置APP信息，初始化方法如下
        //CrashReport.initCrashReport(context, strategy);
    }

    /**
     * 获取进程号对应的进程名
     *
     * @param pid 进程号
     * @return 进程名
     */
    private static String getProcessName(int pid) {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader("/proc/" + pid + "/cmdline"));
            String processName = reader.readLine();
            if (!TextUtils.isEmpty(processName)) {
                processName = processName.trim();
            }
            return processName;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException exception) {
                exception.printStackTrace();
            }
        }
        return null;
    }



}
